/**
 *	单位应用管理
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-28
 */

package com.framework.Model;


import com.framerock.db.DBConn;
import com.framerock.server.Request;
import com.framerock.baselib.Rock;
import com.framework.Flow.Flow;
import com.framework.ACore.Model;

import java.util.ArrayList;
import java.util.Map;


public class ModelFlowunit extends Model {

    public ModelFlowunit(DBConn dbs) {
        super(dbs);
        this.setTable("flowunit");
    }

    public static ModelFlowunit get(DBConn dbs)
    {
        return new ModelFlowunit(dbs);
    }

    public ModelFlowunit setRequests(Request req)
    {
        this.mRequest = req;
        return this;
    }

    /**
     * 统计的角标
     * */
    private int getStotal(String mknum, String pnum, int issx)
    {
        int to = 0;
        if(!Rock.isEmpt(mknum) && issx ==1){
            to = Flow.get(this.nDb, mknum, mRequest).getStotal(pnum);
        }
        return to;
    }

    //读取当前单位下的应用
    public ArrayList<Map<String, String>> getAgent(String cid, Map<String, String> ars)
    {
        ArrayList<Map<String, String>> rows = this.getAll("`cid`="+cid+" AND `status`=1 AND `isxs`=1", "id,mknum,type,name,icons,url,receid,issx", "`sort`");
        String deptpath = ModelDept.get(this.nDb).useraPath(ars);
        ArrayList<Map<String, String>> data = Rock.getMapList();
        Map<String, String> da;
        if(rows!=null)for(int i=0;i<rows.size();i++){
            da      = rows.get(i);
            if(Rock.isInreceid(deptpath, da.get("receid"))) {
                da.remove("receid");
                da.put("stotal", "" + getStotal(da.get("mknum"), da.get("url"), Rock.getMapint(da,"issx")));
                data.add(da);
            }
        }
        return data;
    }

    //判断模块是否存在
    public Boolean exists(String num, String cid)
    {
        int to = this.getCount("`cid`="+cid+" AND `mknum`='"+num+"'");
        return to>0;
    }

    //读取单位模块信息
    public String funitid(String num, String cid)
    {
        Map<String, String> urs = this.getOne("`cid`="+cid+" AND `mknum`='"+num+"'");
        return Rock.getMapString(urs, "id");
    }
}
